// pages/addresslist/addresslist.js
let app=getApp();
let jumpurl=app.jumpurl.url;
let del_id;
let modify_id;
let thisid = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
      hasaddr:false,
      popflag:false,
      addresslist:[{
        id:1,
        name:"zhangsan",
        tel:"13625632563",
        address:"江苏省徐州市鼓楼区黄河北路90号创意产业园"
      },{
          id: 2,
          name: "lisi",
          tel: "16625632563",
          address: "江苏省徐州市鼓楼区黄河北路90号创意产业园A2-1座"
      }],
      moren:0,
      greyflag:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var that=this;
    //  that.setData({
    //    hasaddr:true
    //  })

     //1.请求数据 获取地址列表
    //请求数据
    wx.request({
      url: jumpurl + 'often/index',
      data: {
        openid: app.userList.openid
      },
      success: function (res) {
        console.log(res);
        var changyong = res.data.changyong;
        if (changyong == 0) {
          that.setData({
            hasaddr: false
          })
        } else {

          that.setData({
            hasaddr: true,
            addresslist: res.data.changyong
          })

        }
      },
      fail: function (err) {
        console.log(err);
      }
    })

      //2.
   

  },
  //点击编辑按钮
  eidtBtn:function(e){
    console.log(e);
      var that=this;
    
      var popflag=that.data.popflag;
      
    if (!popflag){
       thisid = e.currentTarget.dataset.id;
        that.setData({
          popflag: true,
          thisid:thisid,
         
        })
      }else{
        if(thisid!=e.currentTarget.dataset.id){
          that.setData({
            popflag: true,
            thisid: e.currentTarget.dataset.id,

          })
        }else{
          that.setData({
            popflag: false
          })
        }
        
      }
     

  },
  //隐藏编辑菜单
  hidepop:function(e){
     var that=this;
  
    that.setData({
      popflag: false
    })
  },

  //修改
  modify:function(e){
    console.log(e);
    modify_id = e.currentTarget.dataset.id;//要修改的id
    wx.navigateTo({
      url: '/pages/edit_addr/edit_addr?id=' + modify_id,
    })
    // wx.redirectTo({
    //   url: '/pages/edit_addr/edit_addr?id=' + modify_id,
    // })
  },

  //新增
  add_addr:function(){
    wx.navigateTo({
      url: '/pages/add_addr/add_addr'
    })
  },
  //删除
  del:function(e){
     
      var that=this;
      console.log("************");
      console.log(e)
       del_id=e.currentTarget.dataset.id;//待删除地址的id
       that.setData({
         greyflag:true,
         delids: e.currentTarget.dataset.id
       })

  },

  /****取消删除****/
  canceled:function(e){
      this.setData({
          greyflag:false
      })
  },

  /*********确认删除******** */
  deled:function(e){
    var delid = e.currentTarget.dataset.id;
    var address_list = this.data.addresslist;
    var linshilist=[];
      //请求后台
     wx.request({
       url: jumpurl+'often/delete',
       data:{
         openid:app.userList.openid,
         id: delid,
       },
       success:function(res){
      
        

       },
       fail:function(err){
          console.log("删除"+err)
       }
     })
    //弹出框关闭

    // for (var i = 0; i < address_list.length;i++){
    //   if (address_list[i].id == delid){
    //     address_list.splice(i,1);
    //   } 
    // }
   var addr_length = address_list.length;
         for(var i=0;i<addr_length;i++){
           if(address_list[i].id!=del_id){
             linshilist.push(address_list[i])
           }
         }
         console.log("******************************")
         console.log(linshilist);
        if(linshilist.length==0){
          this.setData({
            greyflag: false,
            hasaddr:false,
            addresslist: linshilist
           
          })
         
        }else{
          this.setData({
            greyflag: false,
            hasaddr: true,
            addresslist: linshilist
          })
        }
   

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})